﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SubSonic;
using LinFen;

public partial class webUserControl_SupplierAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            string id = "0";
            if(Request["id"]!=null)
            {
                Query q=new Query("supplierInfo");
                q.AddWhere("id", Request["ID"]);
                IDataReader dr = q.ExecuteReader();
                if (dr.Read())
                {
                    tbNumID.Text = dr[1].ToString();
                    AddItems(DDLCatalogID, "sysCatalog", dr[2].ToString(), 0);
                    AddItems(DDLDangCiID, "sysDangci", dr[3].ToString(), 0);
                    AddItems(DDLCityID, "sysCityName", dr[4].ToString(), 0);

                    Query _q = new Query("sysDistrictName");
                    _q.AddWhere("cityid", dr[4].ToString());
                    IDataReader _dr = _q.ExecuteReader();
                    while (_dr.Read())
                    {
                        ListItem li = new ListItem();
                        li.Value = _dr[0].ToString();
                        li.Text = _dr[1].ToString();
                        if(dr[5].ToString()==_dr[0].ToString())
                        {
                            li.Selected = true;
                        }
                        DDLDistrictID.Items.Add(li);
                    }

                    tbSupplierName.Text = dr[7].ToString();

                    tbLinkMan.Text = dr[9].ToString();
                    tbTelPhone.Text = dr[10].ToString();
                    for (int i = -5; i <= 32; i++)
                    {
                        ListItem li = new ListItem();
                        li.Value = i + "层";
                        li.Text = i + "层";
                        if(li.Value==dr[8].ToString())
                        {
                            li.Selected = true;
                        }

                        ddlSuppliername2.Items.Add(li);
                    }
                    tbAddress.Text = dr[11].ToString();
                    AddItems(DDLNumMale, "", dr[16].ToString(), 1);
                    AddItems(DDLNumFemale, "", dr[18].ToString(), 1);
                    AddItems(DDLHongGan, "", dr[20].ToString(), 1);
                    AddItems(DDLBaoJian, "", dr[22].ToString(), 1);
                    AddItems(DDLQiTa, "", dr[24].ToString(), 1);
                    tbBak2.Text = dr[25].ToString();
                    Panel1.Visible = false;
                    Panel2.Visible = false;
                    Panel3.Visible = true;
                    tbSupplierName.Enabled = false;
                    DropDownList1.Enabled = false;
                    DDLCityID.Enabled = false;
                    ddlSuppliername2.Enabled = false;

                }
            }
            else
            {
                tbNumID.Text = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);  
                AddItems(DDLQiTa, "", "0", 1);
                AddItems(DDLNumMale, "", "0", 1);
                AddItems(DDLNumFemale, "", "0", 1);
                AddItems(DDLHongGan, "", "0", 1);
                AddItems(DDLDangCiID, "sysDangci", "2", 0);
                AddItems(DDLCatalogID, "sysCatalog", "0", 0);
                AddItems(DDLBaoJian, "", "0", 1);
                for (int i = -5; i <= 32; i++)
                {
                    ListItem li = new ListItem();
                    li.Value = i + "层";
                    li.Text = i + "层";
                    if(i==1)
                    {
                        li.Selected = true;
                    }
                    ddlSuppliername2.Items.Add(li);
                }
               
                Cookies aCookies = new Cookies();
                int flag = int.Parse(aCookies.getCookies("cFlag"));
                int isType = int.Parse(aCookies.getCookies("cisType"));
                int districtID = int.Parse(aCookies.getCookies("cDistrictID"));
                int cityID = int.Parse(aCookies.getCookies("cCityID"));

                if (flag == 1)
                {
                    AddItems(DDLCityID, "sysCityName", cityID.ToString(), 0);
                    DDLCityID.Enabled = false;

                    DDLDistrictID.Items.Clear();
                    Query q = new Query("sysDistrictName");
                    q.AddWhere("cityid", DDLCityID.SelectedValue);
                    IDataReader dr = q.ExecuteReader();
                    while (dr.Read())
                    {
                        ListItem li = new ListItem();
                        li.Value = dr[0].ToString();
                        li.Text = dr[1].ToString();
                        DDLDistrictID.Items.Add(li);
                    }
                }

                if (flag == 0)
                {
                    AddItems(DDLCityID, "sysCityName", cityID.ToString(), 0);
                    DDLCityID.Enabled = false;

                    DDLDistrictID.Items.Clear();
                    Query q = new Query("sysDistrictName");
                    q.AddWhere("cityid", DDLCityID.SelectedValue);
                    IDataReader dr = q.ExecuteReader();
                    while (dr.Read())
                    {
                        ListItem li = new ListItem();
                        li.Value = dr[0].ToString();
                        li.Text = dr[1].ToString();
                        if (districtID.ToString() == dr[0].ToString())
                        {
                            li.Selected = true;
                        }
                        DDLDistrictID.Items.Add(li);
                    }
                    Panel2.Visible = true;
                }
            }
        }
    }
    protected void btnSub_Click(object sender, EventArgs e)
    {
        if (Request["ID"] != null)
        {
            Query q = new Query("supplierinfo");
            q.AddUpdateSetting("CatalogID", DDLCatalogID.SelectedValue);
            q.AddUpdateSetting("DangciID", DDLDangCiID.SelectedValue);
            q.AddUpdateSetting("districtID", DDLDistrictID.SelectedValue);
            q.AddUpdateSetting("linkman", tbLinkMan.Text);
            q.AddUpdateSetting("Telphone", tbTelPhone.Text);
            q.AddUpdateSetting("Addre", tbAddress.Text);
            q.AddUpdateSetting("bak", tbBak2.Text);
            q.AddWhere("numid", tbNumID.Text);
            q.Execute();
            Response.Redirect("~/Supplierlist.aspx");
        }
        else
        {
            if (DropDownList1.SelectedValue == "0")
            {
                int total = int.Parse(DDLNumMale.SelectedValue) + int.Parse(DDLNumFemale.SelectedValue) +
                            int.Parse(DDLBaoJian.SelectedValue) +
                            int.Parse(DDLHongGan.SelectedValue) + int.Parse(DDLQiTa.SelectedValue);

                SupplierInfo.Insert(tbNumID.Text, int.Parse(DDLCatalogID.SelectedValue),
                                    int.Parse(DDLDangCiID.SelectedValue), int.Parse(DDLCityID.SelectedValue),
                                    int.Parse(DDLDistrictID.SelectedValue), 0,
                                    tbSupplierName.Text, ddlSuppliername2.SelectedValue, tbLinkMan.Text, tbTelPhone.Text,
                                    tbAddress.Text,
                                    total, total, 0, int.Parse(DDLNumMale.SelectedValue),
                                    int.Parse(DDLNumMale.SelectedValue), int.Parse(DDLNumFemale.SelectedValue),
                                    int.Parse(DDLNumFemale.SelectedValue),
                                    int.Parse(DDLHongGan.SelectedValue), int.Parse(DDLHongGan.SelectedValue),
                                    int.Parse(DDLBaoJian.SelectedValue), int.Parse(DDLBaoJian.SelectedValue), int.Parse(DDLQiTa.SelectedValue), int.Parse(DDLQiTa.SelectedValue),
                                    tbBak.Text);
                SupplierInfo.Insert(tbNumID.Text, int.Parse(DDLCatalogID.SelectedValue),
                                    int.Parse(DDLDangCiID.SelectedValue), int.Parse(DDLCityID.SelectedValue),
                                    int.Parse(DDLDistrictID.SelectedValue), 2,
                                    tbSupplierName.Text, ddlSuppliername2.SelectedValue, tbLinkMan.Text, tbTelPhone.Text,
                                    tbAddress.Text,
                                    total, total, 0, int.Parse(DDLNumMale.SelectedValue),
                                    int.Parse(DDLNumMale.SelectedValue), int.Parse(DDLNumFemale.SelectedValue),
                                    int.Parse(DDLNumFemale.SelectedValue),
                                    int.Parse(DDLHongGan.SelectedValue), int.Parse(DDLHongGan.SelectedValue),
                                    int.Parse(DDLBaoJian.SelectedValue), int.Parse(DDLBaoJian.SelectedValue), int.Parse(DDLQiTa.SelectedValue), int.Parse(DDLQiTa.SelectedValue),
                                    tbBak.Text);
                SupplierInfo.Insert(tbNumID.Text, int.Parse(DDLCatalogID.SelectedValue),
                                    int.Parse(DDLDangCiID.SelectedValue), int.Parse(DDLCityID.SelectedValue),
                                    int.Parse(DDLDistrictID.SelectedValue),3,
                                    tbSupplierName.Text, ddlSuppliername2.SelectedValue, tbLinkMan.Text, tbTelPhone.Text,
                                    tbAddress.Text,
                                    total, total, 0, int.Parse(DDLNumMale.SelectedValue),
                                    int.Parse(DDLNumMale.SelectedValue), int.Parse(DDLNumFemale.SelectedValue),
                                    int.Parse(DDLNumFemale.SelectedValue),
                                    int.Parse(DDLHongGan.SelectedValue), int.Parse(DDLHongGan.SelectedValue),
                                    int.Parse(DDLBaoJian.SelectedValue), int.Parse(DDLBaoJian.SelectedValue), int.Parse(DDLQiTa.SelectedValue), int.Parse(DDLQiTa.SelectedValue),
                                    tbBak.Text);
            }
            else
            {
                SupplierInfo.Insert(tbNumID.Text, int.Parse(DDLCatalogID.SelectedValue),
                                    int.Parse(DDLDangCiID.SelectedValue), int.Parse(DDLCityID.SelectedValue),
                                    int.Parse(DDLDistrictID.SelectedValue), int.Parse(DropDownList1.SelectedValue),
                                    tbSupplierName.Text, ddlSuppliername2.SelectedValue, tbLinkMan.Text, tbTelPhone.Text,
                                    tbAddress.Text,
                                    int.Parse(DDLQiTa.SelectedValue), int.Parse(DDLQiTa.SelectedValue), 0, 0, 0, 0, 0, 0,
                                    0, 0, 0, int.Parse(DDLQiTa.SelectedValue), int.Parse(DDLQiTa.SelectedValue),
                                    tbBak.Text);
            }


            Literal1.Text = "<script language=javascript>alert('添加成功！')</script>";
            Response.Redirect("~/Supplierlist.aspx");
        }

    }

    static void AddItems(DropDownList ddl,string tb,string id,int isType)
    {
        if(isType==1)
        {
            for (int i = 0; i <= 50; i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = i.ToString();
                if (id == i.ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }
        else
        {
            Query q = new Query(tb);
            IDataReader dr = q.ExecuteReader();
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Value = dr[0].ToString();
                li.Text = dr[1].ToString();
                if (id == dr[0].ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            } 
        }
        
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "1")
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            lblS.Text = "消防点位数";
        }
        else
        {
            Panel1.Visible = true;
            Panel2.Visible = true;
            lblS.Text = "其他点位数";
        }
    }
    protected void DDLCityID_SelectedIndexChanged(object sender, EventArgs e)
    {
        DDLDistrictID.Items.Clear();
        Query q = new Query("sysDistrictName");
        q.AddWhere("cityid", DDLCityID.SelectedValue);
        IDataReader dr = q.ExecuteReader();
        while (dr.Read())
        {
            ListItem li = new ListItem();
            li.Value = dr[0].ToString();
            li.Text = dr[1].ToString();
            DDLDistrictID.Items.Add(li);
        } 
    }
}
